package com.xyb.service;

import com.xyb.dao.CourseCommentRepository;
import com.xyb.entity.CourseComment;
import com.xyb.entity.CourseCommentVo;
import com.xyb.entity.PageResult;
import com.xyb.entity.query.CourseCommentQuery;
import com.xyb.utils.IdUtils;
import com.xyb.utils.RandomUtils;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@Service
public class CommentService {
    @Resource
    private MongoTemplate mongoTemplate;
    @Resource
    private IdUtils idUtils;
    @Resource
    private CourseCommentRepository courseCommentRepository;

    public String insertComment(CourseComment courseComment) {
        courseComment.setCommentId(idUtils.nextId());   //设置主键
        courseComment.setLikeNum(0);
        courseComment.setUnlikeNum(0);

        courseComment.setParentId(0L);
        courseComment.setStatus(1);
        courseComment.setCreatedDateTime(LocalDateTime.now());

        courseCommentRepository.save(courseComment);
        return "success";
    }

    public List<CourseCommentVo> selectMyCommentListByCourseId(Long courseId, Long userId) {
        Query query = new Query();
        query.addCriteria(Criteria
                .where("courseId").is(courseId)
                .and("userId").is(userId)
                .and("status").is(1));

        query.with(Sort.by("likeNum").descending().and(Sort.by("createdDateTime").descending()));

        List<CourseComment> list = mongoTemplate.find(query, CourseComment.class);

        List<CourseCommentVo> voList = new ArrayList<>();
        list.forEach(i -> {
            //查询用户信息
            CourseCommentVo commentVo = CourseCommentVo.builder()
                    .commentId(i.getCommentId())
                    .courseId(i.getCourseId())
                    .userInfo(null)
                    .content(i.getContent())
                    .likeNum(i.getLikeNum())
                    .unlikeNum(i.getUnlikeNum())
                    .like(false)
                    .unlike(false)
                    .createdDateTime(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm").format(i.getCreatedDateTime()))
                    .userInfo(CourseComment.UserInfo.builder()
                            .userId(idUtils.nextId())
                            .nickName(RandomUtils.getRandomUserName())
                            .headPortraitUrl(RandomUtils.getRandomPicUrl())
                            .build())
                    .build();
            voList.add(commentVo);
        });
        return voList;
    }

    public PageResult<CourseCommentVo> selectCommentListByCourseId(Long courseId, Long userId, CourseCommentQuery courseCommentQuery) {
        Query query = new Query();
        query.addCriteria(Criteria
                .where("courseId").is(courseId)
                .and("status").is(1));

        long count = mongoTemplate.count(query, CourseComment.class);


        query.with(PageRequest.of(courseCommentQuery.getPage() - 1, courseCommentQuery.getRows()));        //第1页,10条

        if (courseCommentQuery.getOrder().equals("new")) {        //按照最新
            query.with(Sort.by("createdDateTime").descending());
        } else if (courseCommentQuery.getOrder().equals("hot")) {  //按照最热
            query.with(Sort.by("likeNum").descending().and(Sort.by("createdDateTime").descending()));
        }


        List<CourseComment> list = mongoTemplate.find(query, CourseComment.class);

        List<CourseCommentVo> voList = new ArrayList<>();
        list.forEach(i -> {
            //查询用户信息
            CourseCommentVo commentVo = CourseCommentVo.builder()
                    .commentId(i.getCommentId())
                    .courseId(i.getCourseId())
                    .userInfo(null)
                    .content(i.getContent())
                    .likeNum(i.getLikeNum())
                    .unlikeNum(i.getUnlikeNum())
                    .like(true)
                    .unlike(false)
                    .createdDateTime(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm").format(i.getCreatedDateTime()))
                    .userInfo(CourseComment.UserInfo.builder()
                            .userId(idUtils.nextId())
                            .nickName(RandomUtils.getRandomUserName())
                            .headPortraitUrl(RandomUtils.getRandomPicUrl())
                            .build())
                    .build();
            voList.add(commentVo);
        });
        return PageResult.result(count, voList);
    }
}
